package com.epoint.webapp.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import com.epoint.webapp.dao.PayrollDAO;
import com.epoint.webapp.entity.Payroll;
import com.epoint.webapp.entity.ProductSales;

public class PayrollDAOImpl implements PayrollDAO{
	private DataSource dataSource;
	private Connection conn = null ;
	private ResultSet rs = null ;
	private PreparedStatement smt = null ;
	
	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}
	public void update(Payroll payroll) {
		// TODO Auto-generated method stub
		String sql = "UPDATE payroll SET payroll = ?,trainingFee = ? WHERE memberAccount = ? "
				+ "AND hr_itemID = ? AND hr_periodID = ? AND employeeRecord=?";
		try {
			conn = dataSource.getConnection();
			smt = conn.prepareStatement(sql);
			smt.setInt(1, payroll.getPayroll());
			smt.setInt(2, payroll.getTrainingFee());
			smt.setString(3, payroll.getAccount());
			smt.setInt(4, payroll.getItemID());
			smt.setInt(5, payroll.getPeriodID());
			smt.setInt(6, payroll.getEmployeeRecord());
			smt.executeUpdate();			
			smt.close();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {}
			}
		}
	}
	
	public List<Payroll> getList(String account) {
		// TODO Auto-generated method stub
		String sql = "SELECT * FROM payroll WHERE memberAccount = ? ORDER BY "
				+ "hr_periodID ASC, hr_itemID ASC ,employeeRecord ASC ";
		List<Payroll> payrollList = new ArrayList<Payroll>();
		try {
			conn = dataSource.getConnection();
			smt = conn.prepareStatement(sql);
			smt.setString(1, account);
			rs = smt.executeQuery();
			while(rs.next()){
				Payroll payroll = new Payroll();
				payroll.setAccount(rs.getString("memberAccount"));
				payroll.setItemID(rs.getInt("hr_itemID"));
				payroll.setPeriodID(rs.getInt("hr_periodID"));
				payroll.setPayroll(rs.getInt("payroll"));
				payroll.setTrainingFee(rs.getInt("trainingFee"));
				payroll.setEmployeeRecord(rs.getInt("employeeRecord"));
				payrollList.add(payroll);
			}
			rs.close();
			smt.close();					
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {}
			}
		}
		return payrollList;	
	}


}
